package com.easy.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.aliyuncs.kms.Endpoint;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ImageUtil {
    private static final String ENDPOINT="oss-cn-beijing.aliyuncs.com";
    private static final String BUCKET_NAME="bigbigyu";
    private static final String ACCESS_KEY_ID="LTAI5tAP7DuxVxdne2buCpgq";
    private static final String ACCESS_KEY_SECRET="Uo5UEXkB3Kx1SUtfvVY4V3ISQ7iypv";
    private static final OSS ossClient=new OSSClientBuilder().build("http://"+ENDPOINT,ACCESS_KEY_ID,ACCESS_KEY_SECRET);
    public static String upload(InputStream is, String fileName)
    {
        String newFileName= UUID.randomUUID().toString()+"-"+fileName.substring(fileName.lastIndexOf("."));


        //请求对象
        PutObjectRequest request=new PutObjectRequest(BUCKET_NAME,newFileName,is);
        //响应对象
        PutObjectResult result=ossClient.putObject(request);
        return "https://"+BUCKET_NAME+"."+ENDPOINT+"/"+newFileName;
    }

    public static void main(String[] args) {
        try {
            MultipartFile mfile = null;
            mfile.getInputStream();


            File file = new File("K:\\liufan.png");
            InputStream is = new FileInputStream(file);
            String newFileName = upload(is, file.getName());
            System.out.println(newFileName);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static OSS getOSSClient() {
        return ossClient;
    }
    public static String getBUCKET_NAME() {
        return BUCKET_NAME;
    }
    public static String getENDPOINT() {
        return ENDPOINT;
    }
    //获取阿里云OSS中的所以图片文件的url列表
    //return图片url列表
    public static List<String> getAllImageUrls() {
        List<String> imageUrls = new ArrayList<>();
        try {
            // 创建列出对象请求
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(BUCKET_NAME);
            ObjectListing objectListing;
            String nextMarker = null;

            do {
                // 设置marker，用于分页获取
                listObjectsRequest.setMarker(nextMarker);
                objectListing = ossClient.listObjects(listObjectsRequest);

                // 遍历所有对象
                for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                    String key = objectSummary.getKey();
                    // 只处理图片文件
                    if (isImageFile(key)) {
                        String url = "https://" + BUCKET_NAME + "." + ENDPOINT + "/" + key;
                        imageUrls.add(url);
                    }
                }

                nextMarker = objectListing.getNextMarker();
            } while (objectListing.isTruncated());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return imageUrls;



    }
    /**
     * 判断文件是否为图片
     * @param fileName 文件名
     * @return 是否为图片
     */
    private static boolean isImageFile(String fileName) {
        if (fileName == null || fileName.isEmpty()) {
            return false;
        }
        String lowerFileName = fileName.toLowerCase();
        return lowerFileName.endsWith(".jpg") ||
                lowerFileName.endsWith(".jpeg") ||
                lowerFileName.endsWith(".png") ||
                lowerFileName.endsWith(".gif") ||
                lowerFileName.endsWith(".bmp");
    }

    /**
     * 删除OSS中的文件
     * @param objectKey 文件key
     */
    public static void deleteImage(String objectKey) {
        try {
            ossClient.deleteObject(BUCKET_NAME, objectKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从URL中提取OSS文件key
     * @param url 完整URL
     * @return 文件key
     */
    public static String extractKeyFromUrl(String url) {
        try {
            String prefix = "https://" + BUCKET_NAME + "." + ENDPOINT + "/";
            if (url != null && url.startsWith(prefix)) {
                return url.substring(prefix.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
